/**
 * server 服务层封装
 * @type {{}}
 * 使用 地址,参数,请求GET,POST
 * server.resource('placeList',{},'POST').then(function(data){
      console.log(data)
    });
 */
var server = {};
var base_api_path = 'http://news.51idy.com/';
server.resource = function(url, param, action) {
  return new Ajaxfactory(url, param, action);
};
function Ajaxfactory(url, param, method, header) {
  this.url = url;
  this.param = param;
  this.method = method;
  this.header = header;
  return this.getData();
};
Ajaxfactory.prototype.getData = function() {
  var _this= this;
  var promise = new Promise(function(resolve, reject) {
      wx.request({
        url: base_api_path + _this.url,
        header: {
            'Content-Type': _this.header ? _this.header : 'application/json'
        },
        method: _this.method ? _this.method : 'GET',
        data: _this.param ?  _this.param : {},
        success: function(res) {
          resolve(res);
        },
        fail:function(res){
          reject(res);
        }
    })
  });
   return promise;
};

module.exports = server;
